package com.iwith.dongmoon.listadpater;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.iwith.dongmoon.activity.EditPhotoActivity;
import com.iwith.dongmoon.activity.R;
import com.iwith.dongmoon.gsonlist.PictureListItem;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.ImageDownloader;
import com.iwith.dongmoon.util.ImageDownloader.Mode;
import com.iwith.dongmoon.util.SessionManager;

public class PhotoGridAdapter extends ArrayAdapter<PictureListItem>{
	private Context mContext;
	private ArrayList<PictureListItem> items;
	private LayoutInflater mInflater;
	private ImageDownloader mImageDownloader;
	private String code;
	private SessionManager mSessionManager;

	public PhotoGridAdapter(Context context, int textViewResourceId, ArrayList<PictureListItem> list, String _code) {
		super(context, textViewResourceId, list);
		// TODO Auto-generated constructor stub

		this.mContext = context;
		this.items = list;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mImageDownloader = new ImageDownloader();
		this.mImageDownloader.setMode(Mode.CORRECT);
		this.code=_code;
		this.mSessionManager=new SessionManager(mContext);
	}
	
	class ViewHolder {
		ImageView iconImage;
	}

	@Override
	public PictureListItem getItem(int position) {
		if(items==null) 	return null;
		else 				return items.get(position);
	}
	
	@Override
	public int getCount(){
		if(items==null)		return 0;
		else				return super.getCount();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.griditem_photo, null);
			holder.iconImage = (ImageView) convertView.findViewById(R.id.griditemphoto_image);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final PictureListItem item = items.get(position);

		if (item != null) {
			if(item.getMember_thumbnail()!=null){
				mImageDownloader.download(item.getMember_thumbnail(), holder.iconImage);
			} else if (item.getBiz_thumbnail()!=null){
				mImageDownloader.download(item.getBiz_thumbnail(), holder.iconImage);
			}
		}
		
		convertView.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String imagepath=null;
				if (code.equals(ApiCode.USERPHOTO) == true
						|| code.equals(ApiCode.MODIFYUSERPHOTO) == true) {
					imagepath = item.getMember_picture();
				} else if (code.equals(ApiCode.BIZPHOTO) == true
						|| code.equals(ApiCode.MODIFYBIZPHOTO) == true) {
					imagepath = item.getBiz_picture();
				}				
				
				Intent intent=new Intent(mContext, EditPhotoActivity.class);
				intent.putExtra("userid", mSessionManager.getUserid());
				intent.putExtra("wr_id", item.getWr_id());
				intent.putExtra("code", code);
				intent.putExtra("imagepath", imagepath);
				mContext.startActivity(intent);
			}
		});
		
		return convertView;
	}
}
